# Maintainer: George Rawlinson <grawlinson@archlinux.org>

pkgbase=firecracker
pkgname=('firecracker' 'firecracker-docs')
pkgver=1.3.1
pkgrel=1
pkgdesc='Secure and fast microVMs for serverless computing'
arch=('loong64' 'x86_64')
url='https://firecracker-microvm.github.io'
license=('Apache' 'BSD')
makedepends=('git' 'rust' 'clang')
options=('!lto')
_commit='94752a7d79993d0db565c19b1469ae45922a8668'
source=("$pkgname::git+https://github.com/firecracker-microvm/firecracker.git#commit=$_commit")
b2sums=('SKIP')

_cargo_target="$CARCH-unknown-linux-gnu"

pkgver() {
  cd "$pkgbase"

  git describe --tags | sed 's/^v//'
}

prepare() {
  cd "$pkgbase"

  # download dependencies
  cargo fetch --locked --target="$_cargo_target"
}

build() {
  cd "$pkgbase"

  cargo build \
    --package firecracker \
    --package jailer \
    --package seccompiler \
    --package rebase-snap \
    --release \
    --frozen \
    --all-features \
    --target-dir=target \
    --target="$_cargo_target"
}

package_firecracker() {
  depends=('gcc-libs')

  cd "$pkgbase"

  # binaries
  find "target/$_cargo_target/release" \
    -maxdepth 1 \
    -executable \
    -type f \
    -exec install -vDm755 -t "$pkgdir/usr/bin/" {} +

  # licenses
  install -vDm644 -t "$pkgdir/usr/share/licenses/$pkgname" \
    LICENSE NOTICE THIRD-PARTY
}

package_firecracker-docs() {
  #arch=('any')
  pkgdesc+=" (documentation)"

  cd "$pkgbase"

  # documentation
  install -vDm644 -t "$pkgdir/usr/share/doc/$pkgbase" ./*.md
  cp -vr docs "$pkgdir/usr/share/doc/$pkgbase"

  # licenses
  install -vDm644 -t "$pkgdir/usr/share/licenses/$pkgname" \
    LICENSE NOTICE THIRD-PARTY
}
